package org.boulderfoodrescue.webapp.server.location;

import java.util.List;
import java.util.logging.Logger;

import org.boulderfoodrescue.webapp.client.rpc.LocationService;
import org.boulderfoodrescue.webapp.server.users.UserAccountsDatabase;
import org.boulderfoodrescue.webapp.shared.Location;
import org.boulderfoodrescue.webapp.shared.UserAccount;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails.AccessLevel;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class LocationServiceImpl extends RemoteServiceServlet implements LocationService{
	private static final Logger log = Logger.getLogger(LocationServiceImpl.class.getName());

	@Override
	public Long saveLocationData(Location location) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute saveLocationData() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		if(location.getDatabaseKey() != null && location.getDatabaseKey() != 0){
			return LocationDatabase.updateLocation(location); 
		}
		return LocationDatabase.addLocation(location).getDatabaseKey();
	}

	@Override
	public boolean removeUser(Location location) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute removeUser() call");
			return false;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail());
			return false;
		}
		return LocationDatabase.removeLocation(location);
	}

	@Override
	public List<Location> getDonors() {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute getDonors() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user == null){
			log.warning("User account is out of date");
			return null;
		}
		else if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail());
			return null;
		}
		return LocationDatabase.getDonors();
	}

	@Override
	public List<Location> getRecipients() {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute getRecipients() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail());
			return null;
		}
		return LocationDatabase.getRecipients();
	}
}
